package com.vilicode.service.impl;

import com.vilicode.bean.Page;
import com.vilicode.bean.PostType;
import com.vilicode.mapper.PostMapper;
import com.vilicode.mapper.PostTypeMapper;
import com.vilicode.service.PostTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class PostTypeServiceImpl implements PostTypeService {
    @Autowired
    public PostMapper postMapper;
    @Autowired
    public PostTypeMapper postTypeMapper;

    @Override
    public List<PostType> queryPostTypes() {
        return postTypeMapper.queryPostTypes();
    }

    @Override
    public String queryPostTypeNameByPostTypeID(int ptid) {
        return null;
    }

    @Override
    //pageNumber=1&ptid=-1
    public Page queryPostByPostTypeID(int ptid, int pageNumber) {
        Page p = new Page();
        p.setPageNumber(pageNumber);
        int totalCount = 0;
        try {
            if(ptid==-1)
                totalCount=postMapper.queryCountOfPosts();//查询记录数
            else
                totalCount = postTypeMapper.queryCountOfPostsByTypeID(ptid);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        p.SetPageSizeAndTotalCount(8, totalCount);
        List list=null;
        try {
            if(ptid==-1)
                list=postMapper.queryPosts((pageNumber-1)*8,8);
            else
                list = postTypeMapper.queryPostsByptid(ptid,(pageNumber-1)*8,8);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        p.setList(list);
        return p;
    }



    }


